/**
 * 医德医风管理科评加扣分项目设置
 * 
 * @Date: 2016-1-25
 * @author: Jackson wang
 * @version: 1.0
 * @since: 1.0
 */

define(["jquery","sysmanMain","valueUtil","msgServ","datagridServ","btnServ"],
		function($,sysmanMain,valueUtil,msgServ,datagridServ,btnServ) {
			
		'use strict';
		
		var rootid = 'dic-dept-subject-win';
		
		var moduCode = 'dicDeptSubject';
		
		var pop_window = '#ethic-dept-pop-win',editBegin = false,editIndex = -1
		
		//临时存放传入参数
		var params ;
		
		var public_method ={		
			
			
			rowClick:function(index,data){
				
				if(window.console)
					console.info('inpatient rowClick event fired ...')
				var _config = sysmanMain.getModuConf('inpatientItemFee');
				if(_config ==null){
					_config = {}					
				}	
				
				_config.initQueryParams = {field:'patient_id',fieldVal:data.patient_id}
				sysmanMain.setModuConf('inpatientItemFee',_config)
				sysmanMain.medicalInpatientItemFeeSelect()
			},
			/**
			 * 初始化新增页面
			 * @param _options.record 列表选中行
			 * @param _options.form 页面form对象
			 * @param _options.rootId 页面根节点
			 */
			initEdit:function(_options){
				if(window.console)
					console.info('dicDeptSubject initEdit event fired ...')
				
				
			}
			,
			
			beforeSubmit:function(){
				if(window.console)
					console.info('dicDeptSubject beforeSave event fired ...')
				var _tid = $('#' + rootid + ' #item_typeid')
				
				var ret = {}
				
				var _tvalue = _tid.combo('getValue');
				if(_tvalue==='1'){
//					$('#' + rootid + ' #item_type').val('加分')
					ret.item_type='加分'
				}else{
//					$('#' + rootid + ' #item_type').val('扣分')
					ret.item_type='扣分'
				}
				
				return ret;
			}
			,
			//脚本弹窗
			openPopPage:function(_options){
				
				params = _options;
				var _url = '/'+sysmanMain.getRootPath()+'/'+moduCode + '/query/popPage.action';
					

				var _param = {
						
					title: '科评加扣分',
					grid : _options.grid,
					window : $(pop_window),
					href : _url ,
					flag : false
				}
				
				datagridServ.windowEdit(_param);
			}
			,
			//弹窗初始化
			initPop:function(){
			
				//获取初始化数据
				var _url = '/'+sysmanMain.getRootPath()+'/'+moduCode + '/query/getValidListDomain.action';
				
				//查询参数设置
				var _param = {
					condition:{is_valid:1}
					
				}
				
				
				//执行查询
				$.ajax({
					url			: _url,
					data		: _param,
					type 		: 'post',
					contentType	: "appliction/json;charset=utf-8",
					success		: private_method.initPopList,
					error : function(jqXHR, textStatus, errorThrown) {
						// $.messager.alert('提示','加载失败,请重新刷新页面');	
						
						msgServ.Msgslide('操作失败,请重新刷新页面，如果问题仍旧存在，请联系系统管理员处理。');
					}
				
				})
				
				
				
			}
			
		}
		
		var private_method ={
			
			/**
			 * 初始化列表界面
			 */
			initPopList: function(data){
				//合并传入数据
				
				var _data = data.rows;
				$.each(_data,function(dj,d){
					if(params.data){
						$.each(params.data,function(pj,p){
							if(p.id === d.id){
								d.dept_score = p.dept_score;
								d.dept_content = p.dept_content;
							}
						})
					}
				})
				
				//初始化datagrid
				
				
				
				var _gridParam = {
						
						// singleSelect:false,
						idField : 'id',
						fitColumns : true,
						data		: _data,
						onClickCell : private_method.editCell,
						columns : [ [
						// {field:'ck',checkbox:"true"},
						{
							field : 'id',
							title : '序号',
							align : 'center',
							width : 20
						}, {
							field : 'name',
							title : '项目名称',
							width : 60,
							align : 'left'
						}, {
							field : 'item_type',
							title : '项目类型',
							width : 20,
							align : 'center'
						}, {
							field : 'time_score',
							title : '单次分值',
							width : 20,
							align : 'center'
						}, {
							field : 'item_score',
							title : '上限分值',
							width : 20,
							align : 'center'
						}, {
							field : 'dept_score',
							title : '科室评分',
							width : 20,
							align : 'center',
							editor: {type:'numberbox',options:{precision:1}}
						} , {
							field : 'dept_content',
							title : '考评备注',
							width : 40,
							align : 'center',
							editor: 'text'
						} ] ],
						
						toolbar		: pop_window + ' .tb-toolbar' 

					}
					
					$(pop_window + ' .dg-list').datagrid(_gridParam);
				
				private_method.bindBtnEvent();
			}
			,
			//行内编辑响应函数
			editCell : function(index, field, value) {
				if (window.console)
					console.info(' editCell fired ...')
				var _gridid = $( pop_window + ' .dg-list')
	
				if (field == 'dept_content' || field == 'dept_score') {
					if (!editBegin) {
						editBegin = true
					}
					_gridid.datagrid('beginEdit', index);
	
					if (editIndex > -1 && (editIndex != index)) {
						editIndex = index
					}
	
				}
			}
			,
			//保存回调
			save:function(){
				
				var _gridid = $( pop_window + ' .dg-list')
				if (editBegin) {
					_gridid.datagrid('acceptChanges');
					_gridid.datagrid('endEdit', editIndex);
					editIndex = -1;
				}
				var _data = _gridid.datagrid('getData')

				
				var ret = {}
				if (_data && _data.rows) {
					
					//循环判断评分是否超出分值上限
					$.each(_data.rows,function(j,o){
						
						if(o.dept_score && (parseFloat(o.dept_score) > parseFloat(o.item_score)) ){
							o.dept_score = o.item_score
						}
					})
					
					ret = $.toJSON(_data.rows)
					
				}
				
				if(params.callback){
					params.callback({
						id:params.id,
						data:_data.rows
					});
					
				}
			}
			
			,
			
			//绑定按钮事件
			bindBtnEvent:function(){
				
				var btn = $(pop_window + ' a.function-btn')
				
				// 绑定按钮事件
				
				//判断按钮是否存在
				if(btn.length>0){
					$(btn).unbind('click');
					$(btn).bind('click', private_method.function_btn_listenner)	
				}
			}
			,
			function_btn_listenner:function(evt){
				
				var bc = {
					'保存'	:	private_method.save	,
					'取消'	: 	function(){
						$(pop_window).window('close');
					}
				}
				btnServ.applyButton({buttonEvt:evt,buttonConfig:bc})
			}
			
		}
			
		
		return public_method;
			
})